﻿using M_ApiResult;
using M_Manufacture.API.Application.Command.WorkOrderCommand;
using M_Manufacture.Domain;
using M_Manufacture.Domain.DTO;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M_Manufacture.API.Controllers
{
    /// <summary>
    /// 工单控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WorkOrdersController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public WorkOrdersController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加工单信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateWorkorder(CreateWorkOrderCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "添加工单信息失败";
                throw;
            }
            return result;

        }
        /// <summary>
        /// 显示工艺信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<WorkOrder>>> GetWEorkorderList([FromQuery] ShowWorkOrderCommand command)
        {
            ApiResult<PageDto<WorkOrder>> result = new ApiResult<PageDto<WorkOrder>>();
            try
            {
                result.Data = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示详情
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<WorkOrder>> GetIdWorkorder([FromQuery] GetIdWorkOrderCommand command)
        {
            ApiResult<WorkOrder> result = new ApiResult<WorkOrder>();
            
            try
            {
result.Data= await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code= -1;
                result.Message = "显示详情信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 工单下发
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> XiaFaWorkorder([FromQuery] XiaFaWorkOrderCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "工单下发失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 关单/暂停
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ZanGuandanWorkorder([FromQuery] ZhantTingWorkOrderCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }

}
